<?php

class App_Methods_Request_GetUserPulpitList {

  /**
   *
   * @var App_Methods_Data_App
   */
  public $app;

  public function __construct($params) {
    $this->app = new App_Methods_Data_App($params->app);
  }

  public function process() {
    $applications = ApplicationsQuery::create()
    ->useUserGroupApplicationsQuery()
    	->filterByUserGroupId(Core_Auth::getInstance()->getUser()->getUserGroupId())
    ->endUse()
    ->useApplicationDataSetsQuery()
    	->filterByStatus(ApplicationDataSets::STATUS_ACCEPTED)
    ->endUse()
    ->leftJoinUserPulpit()
    ->addJoinCondition('UserPulpit', 'UserPulpit.UserId = ?', Core_Auth::getInstance()->getUser()->getId())
    ->with('UserPulpit')
    ->with('ApplicationDataSets')
    ->find();
    $added = false;
    foreach ($applications as $application) {
    	if (!$application->getUserPulpits()->getFirst() instanceof UserPulpit) {
    		$userPulpit = new UserPulpit();
    		$userPulpit->setDefaultData();
    		$application->addUserPulpit($userPulpit);
    		$added = true;
    	}
    }
    if ($added) {
    	$applications->save();
    }
    $response = new App_Methods_Response_GetUserPulpitList(true);
    foreach ($applications as $application) {
    	$response->addApplication($application);
    }
    return $response;
  }

}